package com.atguigu.mediator.smarthouse;

import java.util.HashMap;

/**
 * 具体的中介者类
 */
public class MediatorExt extends Mediator {

	/** 集合，放入所有的同事对象 */
	private HashMap<String, Colleague> colleagueMap;

	private HashMap<String, String> interMap;

	public MediatorExt() {
		colleagueMap = new HashMap<String, Colleague>();
		interMap = new HashMap<String, String>();
	}

	@Override
	public void register(String colleagueName, Colleague colleague) {
		colleagueMap.put(colleagueName, colleague);

		if (colleague instanceof ColleagueForAlarm) {
			interMap.put("Alarm", colleagueName);
		} else if (colleague instanceof ColleagueForCoffeeMachine) {
			interMap.put("CoffeeMachine", colleagueName);
		} else if (colleague instanceof ColleagueForTV) {
			interMap.put("TV", colleagueName);
		} else if (colleague instanceof ColleagueForCurtains) {
			interMap.put("Curtains", colleagueName);
		}
	}

	/**
	 * 具体中介者的核心方法
	 * <pre>
	 * 当 闹钟（CoffeeMachine） 发出消息时，stateChange==0时，表示开始，此时，要求咖啡机启动，电视打开
	 * 当 闹钟（CoffeeMachine） 发出消息时，stateChange==1时，要求咖啡机启动，电视打开
	 * </pre>
	 */
	// 1. 根据得到消息，完成对应任务
	// 2. 中介者在这个方法，协调各个具体的同事对象，完成任务
	@Override
	public void getMessage(int stateChange, String colleagueName) {
		
		System.out.println("--------- start MediatorExt.getMessage()-----------");
		
		// 处理闹钟发出的消息
		if (colleagueMap.get(colleagueName) instanceof ColleagueForAlarm) {
			
			if (stateChange == 0) {
				
				((ColleagueForCoffeeMachine) (colleagueMap.get(interMap.get("CoffeeMachine")))).startCoffee();
				((ColleagueForTV) (colleagueMap.get(interMap.get("TV")))).StartTv();
				
			} else if (stateChange == 1) {
				((ColleagueForTV) (colleagueMap.get(interMap.get("TV")))).StopTv();
			}

		} else if (colleagueMap.get(colleagueName) instanceof ColleagueForCoffeeMachine) {
			((ColleagueForCurtains) (colleagueMap.get(interMap.get("Curtains")))).upCurtains();

		} else if (colleagueMap.get(colleagueName) instanceof ColleagueForTV) {// 如果TV发现消息

		} else if (colleagueMap.get(colleagueName) instanceof ColleagueForCurtains) {
			// 如果是以窗帘发出的消息，这里处理...
		}
		System.out.println("--------- end MediatorExt.getMessage()-----------");
	}

	@Override
	public void sendMessage() {
	}

}
